﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Web;
using System.Web.Caching;

namespace LibraryUtility.Data
{
    /// <summary>
    /// ClassName:CacheHelper
    /// Auther:Leo.wl
    /// Time:2010-04-15
    /// </summary>
    public  class CacheHelper
    {
        private const string AuthorizationCacheDependencyKey = "_::AuthorizationCacheDependencyKey";

        /// <summary>
        /// Initializes a new instance of the <see cref="CacheHelper"/> class.
        /// </summary>
        private CacheHelper()
        {
            ;
        }

        /// <summary>
        /// Gets the authorization cache dependency.
        /// </summary>
        /// <value>The authorization cache dependency.</value>
        private static CacheDependency AuthorizationCacheDependency
        {
            [DebuggerStepThrough()]
            get
            {
                object obj = Get(CacheHelper.AuthorizationCacheDependencyKey);
                if (obj == null)
                    Add(CacheHelper.AuthorizationCacheDependencyKey, new object(), 300, CacheItemPriority.NotRemovable);
                return new CacheDependency(null, new string[] { CacheHelper.AuthorizationCacheDependencyKey });
            }
        }

        #region Get
        /// <summary>
        /// Gets the specified cache key.
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        /// <returns></returns>
        [DebuggerStepThrough()]
        public static object Get(string cacheKey)
        {
            return HttpContext.Current.Cache.Get(cacheKey);
        }
        #endregion

        #region Release
        /// <summary>
        /// 使所有引用AuthorizationCacheDependency的Cache过期
        /// </summary>
        [DebuggerStepThrough()]
        public static void ReleaseAuthorizationCache()
        {
            Release(AuthorizationCacheDependencyKey);
        }
        [DebuggerStepThrough()]
        public static void Release(string cacheKey)
        {
            HttpContext.Current.Cache.Remove(cacheKey);
        }
        #endregion

        #region Add
        /// <summary>
        /// 添加一个缓存项
        /// </summary>
        /// <param name="cacheKey">缓存标识</param>
        /// <param name="cacheValue">缓存内容</param>
        /// <param name="slidingExpirationMinutes">缓存滑动过期时间（分钟）</param>
        public static void Add(string cacheKey, object cacheValue, int slidingExpirationMinutes)
        {
            Add(cacheKey, cacheValue, slidingExpirationMinutes, false);
        }


        /// <summary>
        /// 添加一个缓存项
        /// </summary>
        /// <param name="cacheKey">缓存标识</param>
        /// <param name="cacheValue">缓存内容</param>
        /// <param name="slidingExpirationMinutes">缓存滑动过期时间（分钟）</param>
        /// <param name="cacheItemPriority">缓存优先级</param>
        [DebuggerStepThrough()]
        public static void Add(string cacheKey, object cacheValue, int slidingExpirationMinutes, CacheItemPriority cacheItemPriority)
        {
            Add(cacheKey, cacheValue, slidingExpirationMinutes, false, null, cacheItemPriority);
        }

        /// <summary>
        /// 添加一个缓存项
        /// </summary>
        /// <param name="cacheKey">缓存标识</param>
        /// <param name="cacheValue">缓存内容</param>
        /// <param name="slidingExpirationMinutes">缓存滑动过期时间（分钟）</param>
        /// <param name="dependencyAuthorizationCache">是否依赖AuthorizationCacheDependency</param>
        public static void Add(string cacheKey, object cacheValue, int slidingExpirationMinutes, bool dependencyAuthorizationCache)
        {
            Add(cacheKey, cacheValue, slidingExpirationMinutes, dependencyAuthorizationCache, (string)null);
        }

        /// <summary>
        /// 添加一个缓存项
        /// </summary>
        /// <param name="cacheKey">缓存标识</param>
        /// <param name="cacheValue">缓存内容</param>
        /// <param name="slidingExpirationMinutes">缓存滑动过期时间（分钟）</param>
        /// <param name="dependencyAuthorizationCache">是否依赖AuthorizationCacheDependency</param>
        /// <param name="dependencyCacheKey">依赖的缓存的标识</param>
        [DebuggerStepThrough()]
        public static void Add(string cacheKey, object cacheValue, int slidingExpirationMinutes, bool dependencyAuthorizationCache, string dependencyCacheKey)
        {
            string[] dependencyCacheKeys = null;
            if (dependencyCacheKey != null)
                dependencyCacheKeys = new string[] { dependencyCacheKey };
            Add(cacheKey, cacheValue, slidingExpirationMinutes, dependencyAuthorizationCache, dependencyCacheKeys);
        }

        public static void Add(string cacheKey, object cacheValue, int slidingExpirationMinutes, bool dependencyAuthorizationCache, string[] dependencyCacheKeys)
        {
            Add(cacheKey, cacheValue, slidingExpirationMinutes, dependencyAuthorizationCache, dependencyCacheKeys, CacheItemPriority.Normal);
        }

        /// <summary>
        /// 添加一个缓存项
        /// </summary>
        /// <param name="cacheKey">缓存标识</param>
        /// <param name="cacheValue">缓存内容</param>
        /// <param name="slidingExpirationMinutes">缓存滑动过期时间（分钟）</param>
        /// <param name="dependencyAuthorizationCache">是否依赖AuthorizationCacheDependency</param>
        /// <param name="dependencyCacheKeys">依赖的缓存的标识</param>
        [DebuggerStepThrough()]
        public static void Add(string cacheKey, object cacheValue, int slidingExpirationMinutes, bool dependencyAuthorizationCache, string[] dependencyCacheKeys, CacheItemPriority cacheItemPriority)
        {
            CacheDependency cd = null;
            if (dependencyAuthorizationCache)
            {
                if (dependencyCacheKeys != null)
                    cd = new CacheDependency(null, dependencyCacheKeys, CacheHelper.AuthorizationCacheDependency);
                else
                    cd = CacheHelper.AuthorizationCacheDependency;
            }
            else
            {
                if (dependencyCacheKeys != null)
                    cd = new CacheDependency(null, dependencyCacheKeys);
            }
            Release(cacheKey);
            HttpContext.Current.Cache.Add(cacheKey, cacheValue, cd, Cache.NoAbsoluteExpiration, new TimeSpan(0, slidingExpirationMinutes, 0), cacheItemPriority, null);
        }

        /// <summary>
        /// 添加一个缓存项
        /// </summary>
        /// <param name="cacheKey">缓存标识</param>
        /// <param name="cacheValue">缓存内容</param>
        /// <param name="slidingExpirationMinutes">缓存滑动过期时间（分钟）</param>
        /// <param name="dependency">依赖项</param>
        [DebuggerStepThrough()]
        public static void Add(string cacheKey, object cacheValue, int slidingExpirationMinutes, CacheDependency dependency)
        {
            HttpContext.Current.Cache.Add(cacheKey, cacheValue, dependency, Cache.NoAbsoluteExpiration, new TimeSpan(0, slidingExpirationMinutes, 0), CacheItemPriority.Normal, null);
        }

        /// <summary>
        /// 添加一个缓存项
        /// </summary>
        /// <param name="cacheKey">缓存标识</param>
        /// <param name="cacheValue">缓存内容</param>
        /// <param name="slidingExpirationMinutes">缓存滑动过期时间（分钟）</param>
        /// <param name="dependencyFilename">依赖的文件的文件名</param>
        [DebuggerStepThrough()]
        public static void Add(string cacheKey, object cacheValue, int slidingExpirationMinutes, string dependencyFilename)
        {
            string[] dependencyFilenames = null;
            if (dependencyFilename != null)
                dependencyFilenames = new string[] { dependencyFilename };
            Add(cacheKey, cacheValue, slidingExpirationMinutes, dependencyFilenames);
        }

        /// <summary>
        /// 添加一个缓存项
        /// </summary>
        /// <param name="cacheKey">缓存标识</param>
        /// <param name="cacheValue">缓存内容</param>
        /// <param name="slidingExpirationMinutes">缓存滑动过期时间（分钟）</param>
        /// <param name="dependencyFilenames">依赖的文件的文件名</param>
        [DebuggerStepThrough()]
        public static void Add(string cacheKey, object cacheValue, int slidingExpirationMinutes, string[] dependencyFilenames)
        {
            CacheDependency cd = null;

            if (dependencyFilenames != null)
                cd = new CacheDependency(dependencyFilenames);
            Release(cacheKey);
            HttpContext.Current.Cache.Add(cacheKey, cacheValue, cd, Cache.NoAbsoluteExpiration, new TimeSpan(0, slidingExpirationMinutes, 0), CacheItemPriority.Normal, null);
        }
        #endregion
    }
}
